package com.hejing.bpeldesign.bpelprase;

import com.hejing.bpeldesign.model.BpelNode;
import com.hejing.bpeldesign.model.InvokeNode;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import java.util.UUID;

public class InvokeNodeParser implements NodeParserStrategy {
    @Override
    public BpelNode parse(Element el) {
        String id = UUID.randomUUID().toString();
        String name = el.getAttribute("name");
        String bounds = el.getAttribute("bounds");
        String[] parts = bounds.split(",");
        int x = Integer.parseInt(parts[0]);
        int y = Integer.parseInt(parts[1]);
        int w = Integer.parseInt(parts[2]);
        int h = Integer.parseInt(parts[3]);

        InvokeNode node = new InvokeNode(id, name, el.getTagName(), x, y, w, h);
        node.setDescription(el.getAttribute("description"));
        node.setPartnerLink(el.getAttribute("partnerLink"));
        node.setPartnerLinkType(el.getAttribute("partnerLinkType"));
        node.setOperation(el.getAttribute("operation"));
        node.setOperationDesc(el.getAttribute("operationDesc"));
        node.setIgnoreThrow(Boolean.parseBoolean(el.getAttribute("ignoreThrow")));
        node.setFork(Boolean.parseBoolean(el.getAttribute("fork")));

        NodeList inputs = el.getElementsByTagName("input");
        for (int i = 0; i < inputs.getLength(); i++) {
            node.addInput(((Element) inputs.item(i)).getAttribute("variable"));
        }
        NodeList outputs = el.getElementsByTagName("output");
        if (outputs.getLength() > 0) {
            node.setOutput(((Element) outputs.item(0)).getAttribute("variable"));
        }
        return node;
    }
}
